#version 450 core

layout (location = 0) out vec4 o_Color;
//out int o_EntityID;

struct VertexOutput
{
	vec4 Color;
	vec2 TexCoord;
	float TilingFactor;
	vec2 UV;
};

layout (location = 0) in VertexOutput Output;
layout (location = 4) in float v_TexIndex;
layout (location = 5) in flat int v_EntityID;

uniform mediump vec2 TextureSize0;
uniform lowp float Time; 

uniform sampler2D u_Textures[5];

vec3 palette(float t){
    vec3 a = vec3(0.5, 0.5, 0.5);
    vec3 b = vec3(0.5, 0.5, 0.5);
    vec3 c = vec3(1.0, 1.0, 1.0);
    vec3 d = vec3(0.263, 0.416, 0.557);

    return a + b*cos(6.28318*(c*t+d));
}

void main()
{
	vec4 texColor = Output.Color;
    vec2 uv = Output.TexCoord * Output.TilingFactor;

	switch(int(v_TexIndex))
	{
		case  0: texColor *= texture(u_Textures[0], uv); break;
	}

    float ratio = TextureSize0.x / TextureSize0.y;
    uv = uv * 2.0 - 1.0;
    uv.x *= ratio;
    
    vec2 uv0 = uv;
    vec3 finalColor = vec3(0.0);

    for( float i = 0.0; i < 3.0; i++){
        uv = fract(uv * 1.5) - 0.5;

        float d = length(uv) * exp(-length(uv0));

        vec3 col = palette(length(uv0) + Time * 0.4);

        d = sin(d*8.0 + Time)/4.0;
        d = abs(d);

        d = 0.01 / d;

        finalColor += col * d;
    }

    texColor.rgb += finalColor;

	if (texColor.a == 0.0)
		discard;

	o_Color = texColor;
	//o_Color = vec4(uv.x, uv.y, 0.0, 1.0);
}
